﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGCourt.Logic.BLL;
using LGCourt.Logic.NBL.DBEntities;
using KMFrame.Permission.Util;
using LGCourt.Logic.Common;
using System.Web.Security;
using System.Text;

namespace lgweb.control.inside
{
    public partial class Usermenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!SessionBox.CheckUserSession())
                {
                    Response.Redirect("/inside/login.aspx");
                }
                GetSSODomain();
            }
        }
        private void GetSSODomain()
        {
            TimeSpan tp = (DateTime.Now - DateTime.Parse("1970-01-01"));
            UserInfo userBLL = new UserInfo();
            pUserLinkInfo userParam = new pUserLinkInfo();
            userParam.UserName = SessionBox.GetUserSession().UserName;
            pUserLinkInfo userLinkInfo = userBLL.GetUserLinkInfo(userParam);
            string userName = SessionBox.GetUserSession().UserName;
            string sysUserID = SessionBox.GetUserSession().UserID.ToString();
            byte[] bytes = Encoding.Default.GetBytes(userName);
            string decodeUserName = Convert.ToBase64String(bytes);
            string uts = Utils.GetTimestamp();
            this.ZHYWDomain.HRef = GetConfigDomain("ZHYWDomain", decodeUserName, GetUserID(userLinkInfo, "ZHYWDomain", sysUserID), true);
            this.hdOADomain.Value = GetConfigDomain("OADomain", userName, sysUserID, false);// System.Configuration.ConfigurationManager.AppSettings["OADomain"];            
            this.SZFTDomain.HRef = GetConfigDomain("SZFTDomain", null, "", false) + "?username=" + userName + "&timestamp=" + Convert.ToInt64(tp.TotalMilliseconds).ToString();
            this.CMPDomain.HRef = GetConfigDomain("CMPDomain", userName, GetUserID(userLinkInfo, "CMPDomain", sysUserID), true);
            this.YNJKDomain.HRef = GetConfigDomain("YNJKDomain", userName, sysUserID, false);
            this.AJPCDomain.HRef = GetConfigDomain("AJPCDomain", userName, sysUserID, false);
            this.SJJHDomain.HRef = GetConfigDomain("SJJHDomain", null, "", false) + "?UID=" + sysUserID + "&UTS=" + uts + "&UKEY=" + MD5(sysUserID + uts + "a312b8cd2216132e6fg5718h39494601");
            this.KQGLDomain.HRef = GetConfigDomain("KQGLDomain", userName, GetUserID(userLinkInfo, "KQGLDomain", sysUserID), true);
            this.GJZFDADomain.HRef = GetConfigDomain("GJZFDADomain", userName, sysUserID, false);
        }

        private string GetUserID(pUserLinkInfo userLinkInfo, string domainName, string defaultName)
        {
            if (userLinkInfo == null)
                return defaultName;
            string result = defaultName;
            switch (domainName)
            {
                case "ZHYWDomain":
                    result = userLinkInfo.ZHYWXT;
                    break;
                case "SZTSGDomain":
                    result = userLinkInfo.SZTSG;
                    break;
                case "SGYDomain":
                    result = userLinkInfo.SGYNBW;
                    break;
                case "SZYDomain":
                    result = userLinkInfo.SZYGZW;
                    break;
                case "CMPDomain":
                    result = userLinkInfo.CMPCXXT;
                    break;
                case "SZFTDomain":
                    result = userLinkInfo.SZFTGLXT;
                    break;
                case "FLFGDomain":
                    result = userLinkInfo.FLFGCXXT;
                    break;
                case "SJFYDomain":
                    result = userLinkInfo.SJFYYJXT;
                    break;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = defaultName;
            }

            return result;
        }

        private string GetConfigDomain(string domainName, string username, string userid, bool isAddPrm)
        {
            string result = System.Configuration.ConfigurationManager.AppSettings[domainName];
            if (isAddPrm)
            {
                if (!string.IsNullOrEmpty(result) && result.IndexOf("?") > 0)
                {
                    result = "&m=" + Server.UrlEncode(username) + "&p=" + Server.UrlEncode(userid);
                }
                else
                {
                    result = "?m=" + Server.UrlEncode(username) + "&p=" + Server.UrlEncode(userid);
                }
            }
            return result;
        }
        public static string MD5(string s)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
        }
    }
}